package com.fs.activiti5.handler;

import java.util.Arrays;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.fs.activiti5.service.ActivitiService;
import com.fs.base.utils.ApplicationUtils;
import com.fs.jzzn.param.QueryMap;
import com.fs.jzzn.pojo.Project;
import com.fs.jzzn.repository.ProjectRepository;

/**
 * @author TiddlerCJ
 *
 */
public class ProjectTaskHandler implements TaskListener {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ActivitiService activitiService;

	public void notify(DelegateTask delegateTask) {
		ActivitiService activitiService = (ActivitiService) ApplicationUtils.getBean("activitiService");
		String taskName = delegateTask.getName();
		
		//更新工程还支出表单状态
		RuntimeService runtimeService = activitiService.getRuntimeService();
		Object projectId = runtimeService.getVariable(delegateTask.getExecutionId(), "projectId");
		if(projectId!=null){
			ProjectRepository repository = activitiService.getProjectRepository();
			Project project = repository.findOne(projectId.toString());
			project.setStatus(taskName);
			repository.save(project);
			
			String orgNo = project.getOrgNo();
			String inputUser = QueryMap.projectCandidateUserMap.get(orgNo).getString(taskName);
			if(StringUtils.isNotBlank(inputUser)){
				String [] candidateUsers=  inputUser.split(",") ;  
				delegateTask.addCandidateUsers( Arrays.asList(candidateUsers));
			}
		}
	}
}
